/**
 * 比赛步骤模型
 *
 * 任务一
 * 根据比赛查出比赛步骤
 *
 */

module.exports = class extends think.Model
{

    get relation()
    {
        return {
            stepsaward: {
                type: think.Model.HAS_MANY,
                key: 'PK_CompetitionStepsGuid',
                fKey: 'FK_CompetitionStepsGuid'
            }, competitionstepstojudger: {
                type: think.Model.HAS_MANY,
                key: 'PK_CompetitionStepsGuid',
                fKey: 'FK_CompetitionStepsGuid'
            }, competition: {
                type: think.Model.HAS_ONE,
                key: 'FK_CompetitionGuid',
                fKey: 'PK_CompetitionGuid'
            }
            , competitionsteptoworks: {
                type: think.Model.HAS_MANY,
                key: 'PK_CompetitionStepsGuid',
                fKey: 'FK_CompetitionStepsGuid'
            }
        }
    }

    get pk()
    {
        return 'PK_CompetitionStepsGuid';
    }

    /**
     * 根据比赛查出比赛步骤
     * @param PK_CompetitionGuid
     * @param field
     * @returns {Promise.<*>}
     */
    async getCPStepsByCPGuid(PK_CompetitionGuid, field = 'PK_CompetitionStepsGuid')
    {
        let where = {'FK_CompetitionGuid': PK_CompetitionGuid}
        return await this.where(where).field(field).select();
    }

    //获取评委和裁判
    async getGradeAndJudgerByStepGuid(stepguid)
    {
        let res = await this.setRelation(false).alias('step').join({
            competitionsteptoworks: {
                join:'inner',
                as: 'stepwork',
                on: ['PK_CompetitionStepsGuid', 'FK_CompetitionStepsGuid']
            },
            grade: {
                join:'inner',
                as: 'grade',
                on: ['stepwork.PK_WorksNoGuid', 'FK_WorksNoGuid']
            },
            judger: {
                join:'inner',
                as: 'jr',
                on: ['grade.FK_JudgerGuid', 'PK_JudgerGuid']
            }

        }).select();
        let a = 2;
    }
};